package com.scalar.chiptrack.operations.dataentry.rmaReceive.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.dataentry.rmaReceive.forms.RMAReceiveForm;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.reports.dao.DataDumpReportsDAO;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.ExcelUtils;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 28 May, 2009
 * Time: 7:08:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class RMAReceiveReportCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/rmaReceive.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        RMAReceiveForm rmaReceiveForm = (RMAReceiveForm) form;

        HashMap<String, String> errors  = new HashMap<String, String>();
        HashMap<String, String> info    = new HashMap<String, String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);
        
        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);
        Workbook workbook = null;
        String fileName = "";
        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        try
        {
            if ( "receiveReport".equalsIgnoreCase(action))
            {
                if ( user.getVendorId() > 0 ) // for VENDOR INTERFACE
                {
                    if (rmaReceiveForm.getLocationId() == 0 )
                    {
                        ArrayList<LabelValueBean> vendorList = VendorDAO.getVendorNamesByProcessByUserType (null,LOCATION_TYPE_FG,user );
                        if ( vendorList != null && vendorList.size() > 0 )
                        {
                            rmaReceiveForm.setLocationId( Integer.parseInt ( ( (LabelValueBean)vendorList.get(0) ).getValue() ) );
                        }
                        else
                        {
                            rmaReceiveForm.setLocationId( -1 );
                        }
                    }
                }
                request.setAttribute("customersList", CustomerDAO.getAllCustomerNamesNoStringsAttached() );
                request.setAttribute("partNumberList", ProductDao.loadAllPartsForProcessTypeByPsType( LOCATION_TYPE_FG ) );
                request.setAttribute("locationList",   VendorDAO.getVendorNamesByProcessByUserType (null,LOCATION_TYPE_FG,user ) );
            }
            else if ("receiveReportExcel".equalsIgnoreCase(action) )
            {
                ArrayList rmaReceiveList = DataDumpReportsDAO.getRMAReceiveReport ( null, rmaReceiveForm );

                workbook =  DataDumpReportsHelper.getWorkbook( rmaReceiveList, "RMA Receive Report", null,null,null );
                //workbook = DataDumpReportsHelper.formatWipBacklog( workbook );
                fileName = "RMA Receive Report-" + DateUtil.getTimeStamp() ;

                //To GENERATE THE EXCEL REPORT
                ExcelUtils.generateExcelReport( workbook, fileName, response );
            }
       }
       catch( Exception e)
       {

          errors.put( "Could not Get RMA Receive Report details", null);
          e.printStackTrace();
       }

        return mapping.findForward("rmaReceiveReport");
    }

}